##

rm(list = ls())

library(tidyverse)
library(knitr)
library(kableExtra)

## Load data sets

res <- read_rds("data/Results_MediaConsumption.rds") %>%
    mutate(period = 0)

res_ll <- read_rds("data/Results_MediaConsumption_Dynamic.rds")

##

res <- bind_rows(
    res_ll,
    res
) %>%
    mutate(fe = dplyr::recode(fe,
        `none` = "Base model",
        `year` = "Year FE",
        `year_covars` = "Covars + Year FE"
    )) %>%
    mutate(fe = factor(fe, levels = c(
        "Base model",
        "Year FE",
        "Covars + Year FE"
    ))) %>%
    mutate(
        conf.low90 = estimate - qnorm(0.95) * std.error,
        conf.high90 = estimate + qnorm(0.95) * std.error
    )

# New df

res_full <- res

## Rename outcomes

o_list <- c(
    "TZ_u_faz_nw_bin",
    "TZ_u_bild_nw_bin",
    "TZ_u_sz_nw_bin", "TZ_u_welt_nw_bin",
    "index_nw_ratio_bin_national", "index_nw"
)

## ##

o_labs_diff <- o_list %>%
    paste0(., "_mean_diff")
o_labs_proper <- o_list

## Make df

dict_df <- data.frame(
    outcome = c(
        o_labs_diff
    ),
    outcome_proper = o_labs_proper
)


## Prep for table

res_table <- res_full %>%
    filter(str_detect(term, "Decrease")) %>%
    left_join(dict_df) %>%
    dplyr::select(-outcome) %>%
    dplyr::rename(outcome = outcome_proper) %>%
    filter(!is.na(outcome)) %>%
    mutate(outcome = as.character(outcome))

## Rename

res_table <- res_table %>%
    mutate(outcome_proper = dplyr::recode(outcome,
        `index_nw` = "National newspaper\nconsumption",
        `index_nw_ratio_bin_national` = "National newspaper\nconsumption (relative)"
    ))

## Data for table

df_table <- res_table %>%
    filter(fe == "Year FE") %>%
    filter(outcome %in% c(
        "index_nw",
        "index_nw_ratio_bin_national"
    )) %>%
    arrange(outcome, period) %>%
    mutate(est_std = estimate / dv_sd) %>%
    dplyr::select(
        period, estimate, std.error, est_std, dv_sd,
        p.value, n
    ) %>%
    mutate(period = ifelse(period > -1, period + 1, period)) %>%
    filter(period %in% -2:2)

## Tble A.12 (top half)

kable(df_table,
    "latex",
    longtable = F,
    booktabs = T, col.names = c(
        "Election rel. to exit",
        "Estimate",
        "SE",
        "Estimate (in SD)",
        "SD of DV",
        "P",
        "N"
    ),
    linesep = "",
    caption = "Estimates for national news consumption outcome",
    label = "tab:mlfz_appendix",
    escape = F, digits = 3
) %>%
    kable_styling(
        latex_options = c("repeat_header"),
        font_size = 10
    ) %>%
    # collapse_rows() %>%
    row_spec(0, bold = T) %>%
    column_spec(1, width = "4.5cm") %>%
    kable_styling(latex_options = "HOLD_position") %>%
    pack_rows("National news consumption (absolute)", 1, 4,
        hline_after = F
    ) %>%
    pack_rows("National news consumption (relative)", 5, 8,
        hline_after = F
    )

## With covars

df_table_covs <- res_table %>%
    filter(fe == "Covars + Year FE") %>%
    filter(outcome %in% c(
        "index_nw",
        "index_nw_ratio_bin_national"
    )) %>%
    arrange(outcome, period) %>%
    mutate(est_std = estimate / dv_sd) %>%
    dplyr::select(
        period, estimate, std.error, est_std, dv_sd,
        p.value, n
    ) %>%
    mutate(period = ifelse(period > -1, period + 1, period)) %>%
    filter(period %in% -2:2)

## Table A.12 (bottom half)

kable(df_table_covs,
    "latex",
    longtable = F,
    booktabs = T, col.names = c(
        "Election rel. to exit",
        "Estimate",
        "SE",
        "Estimate (in SD)",
        "SD of DV",
        "P",
        "N"
    ),
    linesep = "",
    caption = "Estimates for national news consumption outcome",
    label = "tab:mlfz_appendix",
    escape = F, digits = 3
) %>%
    kable_styling(
        latex_options = c("repeat_header"),
        font_size = 10
    ) %>%
    # collapse_rows() %>%
    row_spec(0, bold = T) %>%
    column_spec(1, width = "4.5cm") %>%
    kable_styling(latex_options = "HOLD_position") %>%
    pack_rows("National news consumption (absolute)", 1, 4,
        hline_after = F
    ) %>%
    pack_rows("National news consumption (relative)", 5, 8,
        hline_after = F
    )




